package com.goldwind.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;

import java.time.Duration;
import java.util.Properties;

@Configuration
@ComponentScan(
        value = "com.goldwind",
        excludeFilters = @ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                classes = Controller.class
        )

)
@PropertySource("classpath:jdbc.properties")
@PropertySource("classpath:email.properties")
public class SpringConfig {
        @Value("${spring.redis.host}")
        private String host;

        @Value("${spring.redis.port}")
        private int port;


        @Bean
        public JavaMailSender javaMailSender() {
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

                // 基础SMTP配置
                mailSender.setHost("smtp.qq.com");
                mailSender.setPort(587);
                mailSender.setUsername("3520174818@qq.com");
                mailSender.setPassword("poyzscjblazhchbd");
                mailSender.setDefaultEncoding("UTF-8");
                mailSender.setProtocol("smtp");
                // SMTP协议属性配置
                Properties props = mailSender.getJavaMailProperties();
                props.put("mail.transport.protocol", "smtp");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.starttls.required", "true");

                // 超时设置
                props.put("mail.smtp.connectiontimeout", 10000);
                props.put("mail.smtp.timeout", 10000);
                props.put("mail.smtp.writetimeout", 10000);

                // 调试模式
                props.put("mail.debug", "true");
                return mailSender;
        }

        @Bean
        public RedisConnectionFactory redisConnectionFactory() {

                RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
                config.setHostName(host);
                config.setPort(port);

                LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                        .commandTimeout(Duration.ofSeconds(3))
                        .shutdownTimeout(Duration.ofMillis(100))
                        .build();

                return new LettuceConnectionFactory(config, clientConfig);

        }

        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {
                RedisTemplate<String, String> template = new RedisTemplate<>();
                template.setConnectionFactory(connectionFactory);

                // 使用StringRedisSerializer来序列化和反序列化redis的key/value
                template.setKeySerializer(new StringRedisSerializer());
                template.setValueSerializer(new StringRedisSerializer());
                template.setHashKeySerializer(new StringRedisSerializer());
                template.setHashValueSerializer(new StringRedisSerializer());

                template.afterPropertiesSet();
                return template;
        }
}
